package com.bai.edu.controller;


import com.bai.edu.entity.Teacher;
import com.bai.edu.entity.vo.TeacherQuery;
import com.bai.edu.service.TeacherService;
import com.bai.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-07-09
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师管理")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有教师数据
     *
     * @return
     */
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        List<Teacher> teacherList = teacherService.list(null);
        return R.ok().data("items", teacherList);
    }

    /**
     * 讲师逻辑删除
     */
    @ApiOperation(value = "讲师逻辑删除")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.erro();
        }
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "讲师分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        Page<Teacher> page = new Page<>(current, limit);
        teacherService.page(page, null);
        long total = page.getTotal();
        List<Teacher> records = page.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }

    /**
     * 条件查询
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> page = new Page<>(current, limit);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        String begin = teacherQuery.getBegin();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.like("level", level);
        }

        teacherService.page(page, wrapper);
        long total = page.getTotal();
        List<Teacher> records = page.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);

    }

    /**
     * 添加讲师
     */
    @PostMapping("addTeacher")
    private R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok();
        } else {
            return R.erro();
        }
    }

  //TODO:修改讲师信息
    /**
     * 根据讲师ID进行查询
     */
    @GetMapping("getTeacer/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    /**
     * 讲师修改
     */
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok();
        } else {
            return R.erro();
        }
    }
}

